<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/18
 * Time: 下午8:50
 */

class PlatformController extends AuthBaseController {

    public function beforeAction($action){
        $this->breadCrumb = PubFunc::t('平台信息管理');
        return parent::beforeAction($action);
    }

    public function index(){

        $filter = new PubAccFilter();
        $grid   = new PubGrid();

        $filter->addFilter('search', 'input', PubFunc::t('平台名'))
            ->addButton('/auth/?r=platform/add', '<i class="fa fa-plus"></i>', 'btn btn-success');

        $params = $filter->get();

        // 数据展示
        $header = PlatformService::indexHeader();
        $data   = PlatformService::indexData($params);
        $grid->addGrid($header, $data);

        $this->out['grid']      = $grid->show();
        $this->out['filter']    = $filter->show();

        // 设置标题
        $this->setTitle('平台列表', 'Platform Info');
    }

    public function add(){
        $form = new PubForm();

        $gameOptions = GameService::getOptions();

        $form->addItem('src', 'input', PubFunc::t('平台简称'))
            ->addItem('name', 'input', PubFunc::t('平台名称'))
            ->addItem('game', 'checkbox', PubFunc::t('下属游戏'), array('options' => $gameOptions))
            ->addOperation('提交', 'input', 'submit', 'btn btn-primary')
            ->addOperation('返回', 'a', '/auth/?r=platform/index', 'btn btn-default')
            ->setAction('/auth/?r=platform/doAdd')
            ->setName('platform');

        $this->out['form'] = $form->show();

        $this->setTitle('新增平台', 'Add Platform');
    }

    public function doAdd(){
        $platform = $this->R('platform');
        $result = PlatformService::add($platform);
        if($result !== false){
            $this->showMessage(PubFunc::t('添加成功'), "/auth/?r=platform/index");
        }else{
            $this->showMessage(PubFunc::t('添加失败'));
        }
    }

    public function edit(){
        $form = new PubForm();

        $id = $this->R('id');
        $platform = PlatformService::getPlatformById($id);

        $gameOptions = GameService::getOptions();

        $form->addItem('src', 'input', PubFunc::t('平台代号'), array('value' => $platform['PlatformSrc']))
            ->addItem('name', 'input', PubFunc::t('平台名称'), array('value' => $platform['PlatformName']))
            ->addItem('game', 'checkbox', PubFunc::t('下属游戏'), array('options' => $gameOptions, 'value' => PlatformGameMapService::getGameByPlatform($id)))
            ->addValue('platform[id]', $id)
            ->addOperation('提交', 'input', 'submit', 'btn btn-primary')
            ->addOperation('返回', 'a', '/auth/?r=platform/index', 'btn btn-default')
            ->setAction('/auth/?r=platform/doEdit')
            ->setName('platform');

        $this->out['form'] = $form->show();

        $this->setTitle('编辑平台', 'Edit System');
    }

    public function doEdit(){
        $platform = $this->R('platform');
        $result = PlatformService::edit($platform);
        if($result !== false){
            $this->showMessage(PubFunc::t('修改成功'), '/auth/?r=platform/index');
        }else{
            $this->showMessage(PubFunc::t('修改失败'));
        }
    }

    public function grant(){

    }

} 